package org.jeecg.modules.online.cgreport.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.DialectFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.jeecg.common.system.vo.DynamicDataSourceModel;
import org.jeecg.common.util.ReflectHelper;
import org.jeecg.common.util.dynamic.db.DataSourceCachePool;
import org.jeecg.common.util.dynamic.db.DynamicDBUtil;
import org.jeecg.modules.online.cgform.utils.CgformUtil;
import org.jeecg.modules.online.p002a.ConditionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* renamed from: org.jeecg.modules.online.cgreport.c.b */
/* loaded from: hibernate-re-3.7.1-RC.jar:org/jeecg/modules/online/cgreport/c/b.class */
public class CgReportSqlUtil {

    /* renamed from: d */
    private static final Logger f506d = LoggerFactory.getLogger(CgReportSqlUtil.class);

    /* renamed from: a */
    public static final String f507a = " where ";

    /* renamed from: b */
    public static final String f508b = " and ";

    /* renamed from: c */
    public static final String f509c = " or ";

    /* renamed from: a */
    public static String m100a(String str) {
        String replace;
        String replaceAll = str.replaceAll("(?i) where ", f507a).replaceAll("(?i) and ", f508b).replaceAll("(?i) or ", " or ");
        Matcher matcher = Pattern.compile("(,\\s*|\\s*(\\w|\\.)+\\s*[^, ]+ *\\S*)\\$\\{\\w+\\}\\S*").matcher(replaceAll);
        while (matcher.find()) {
            String group = matcher.group();
            f506d.debug("${}匹配带参SQL片段 ==>" + group);
            if (group.indexOf(f507a) != -1) {
                replace = replaceAll.replace(group, group.substring(0, group.indexOf(f507a)) + " where 1=1");
            } else if (group.indexOf(f508b) != -1) {
                String substring = group.substring(group.indexOf("and"));
                if (substring.indexOf("(") > 0) {
                    replace = replaceAll.replace(substring.substring(substring.indexOf("(") + 1), " 1=1 ");
                } else {
                    replace = replaceAll.replace(substring, "and 1=1");
                }
            } else if (group.indexOf(" or ") != -1) {
                String substring2 = group.substring(group.indexOf("or"));
                if (substring2.indexOf("(") > 0) {
                    replace = replaceAll.replace(substring2.substring(substring2.indexOf("(") + 1), " 1=1 ");
                } else {
                    replace = replaceAll.replace(substring2, "or 1=1");
                }
            } else if (group.startsWith(CgformUtil.f233E)) {
                replace = replaceAll.replace(group, " ,1 ");
            } else {
                replace = replaceAll.replace(group, " 1=1 ");
            }
            replaceAll = replace;
            f506d.debug("${}替换后结果 ==>" + replaceAll);
        }
        return replaceAll.replaceAll("(?i)\\(\\s*1=1\\s*(AND|OR)", "(").replaceAll("(?i)(AND|OR)\\s*1=1", "");
    }

    /* renamed from: a */
    public static Map<String, Object> m97a(HttpServletRequest httpServletRequest) {
        String str;
        //String[] strArr;
        Map <String, String[]>  parameterMap = httpServletRequest.getParameterMap();
        Map hashMap = new HashMap(5);
        String str2 = "";
        for (Map.Entry entry : parameterMap.entrySet()) {
            String str3 = (String) entry.getKey();
            Object value = entry.getValue();
            if ("_t".equals(str3) || null == value) {
                str = "";
            } else if (value instanceof String[]) {
                for (int i = 0; i < ((String[]) value).length; i++) {
                    str2 = ",";
                }
                str = str2.substring(0, str2.length() - 1);
            } else {
                str = value.toString();
            }
            str2 = str;
            hashMap.put(str3, str2);
        }
        return hashMap;
    }

    /* renamed from: b */
    public static boolean m96b(String str) {
        return str.toLowerCase().indexOf("select") == 0;
    }

    /* renamed from: c */
    public static String m95c(String str) {
        return String.format("SELECT COUNT(1) \"total\" FROM ( %s ) temp_count", str);
    }

    /* renamed from: a */
    public static Map<String, Object> m99a(String str, String str2) {
        Map<String, Object> map = null;
        IDialect dialect = DialectFactory.getDialect(JdbcUtils.getDbType(DataSourceCachePool.getCacheDynamicDataSourceModel(str).getDbUrl()));
        if (str2.toUpperCase().contains("LIMIT") || str2.toUpperCase().contains("OFFSET")) {
            List findList = DynamicDBUtil.findList(str, str2, new Object[0]);
            if (findList != null && findList.size() > 0) {
                map = (Map) findList.get(0);
            }
        } else {
            Page page = new Page(1L, 1L);
            DialectModel buildPaginationSql = dialect.buildPaginationSql(str2, page.offset(), page.getSize());
            String dialectSql = buildPaginationSql.getDialectSql();
            if (dialectSql.contains("?")) {
                long longValue = ((Long) ReflectHelper.getFieldVal("firstParam", buildPaginationSql)).longValue();
                map = dialectSql.length() - dialectSql.replaceAll("\\?", "").length() == 1 ? (Map) DynamicDBUtil.findOne(str, dialectSql, new Object[]{Long.valueOf(longValue)}) : (Map) DynamicDBUtil.findOne(str, dialectSql, new Object[]{Long.valueOf(longValue), Long.valueOf(((Long) ReflectHelper.getFieldVal("secondParam", buildPaginationSql)).longValue())});
            } else {
                map = (Map) DynamicDBUtil.findOne(str, dialectSql, new Object[0]);
            }
        }
        return map;
    }

    /* renamed from: a */
    public static List<Map<String, Object>> m98a(String str, String str2, String str3, int i, int i2, Map<String, Object> map) {
        DynamicDataSourceModel cacheDynamicDataSourceModel = DataSourceCachePool.getCacheDynamicDataSourceModel(str2);
        String str4 = str3;
        DialectModel dialectModel = null;
        if (!Boolean.valueOf(str).booleanValue()) {
            IDialect dialect = DialectFactory.getDialect(JdbcUtils.getDbType(cacheDynamicDataSourceModel.getDbUrl()));
            Page page = new Page(i, i2);
            dialectModel = dialect.buildPaginationSql(str3, page.offset(), page.getSize());
            str4 = dialectModel.getDialectSql();
        }
        if (str4.contains("?")) {
            long longValue = ((Long) ReflectHelper.getFieldVal("firstParam", dialectModel)).longValue();
            long longValue2 = ((Long) ReflectHelper.getFieldVal("secondParam", dialectModel)).longValue();
            if (str4.length() - str4.replaceAll("\\?", "").length() == 1) {
                str4 = ConditionHandler.m578a(str4, longValue);
            } else {
                str4 = ConditionHandler.m577a(str4, longValue, longValue2);
            }
        }
        return DynamicDBUtil.findListByNamedParam(str2, str4, map);
    }
}
